#Region " Imports "

Imports System
Imports System.Data
Imports BlazeApps.Library.Polls
Imports System.Globalization

#End Region

Partial Class Usercontrols_Polls_PollList
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Public Property PollsByUser() As Boolean
        Get
            Return GetBooleanFromVS("PollsByUser")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("PollsByUser", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Polls List")

        If Me.Page.IsPostBack = False Then
            'Set Default Sort Order and Direction
            Me.sortCriteria = "createdate"
            Me.sortDir = "desc"

            LoadPage()
        End If

        ConfigBaseNavCtrls()

        If Me.Page.IsPostBack = False Then        
            BindDataItems()
        End If

        LoadSettings()

        Me.uxAddPollPanel.Visible = ((IsUserAuthenticated And EnableUserPolls) OrElse IsPollModerator())

    End Sub

#End Region

#Region " Save Data "


#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Private Sub LoadSettings()
        MostViewedPanel.Visible = DisplayMostViewedPolls
        MostCommentsPanel.Visible = DisplayMostCommentsPolls
        HighestRankedPanel.Visible = DisplayHighestRankedPolls

        MostViewedPanel.Enabled = DisplayMostViewedPolls
        MostCommentsPanel.Enabled = DisplayMostCommentsPolls
        HighestRankedPanel.Enabled = DisplayHighestRankedPolls
    End Sub

    Public Overloads Overrides Sub BindDataItems()
        Dim List As New PollList
        Dim DT As New DataTable
        Dim DV As New DataView

        If GetBooleanFromQueryString("UsersPolls") OrElse PollsByUser Then
            DT = List.GetPollQuestions(Membership.GetUser(GetStringFromQueryString("userid")).ProviderUserKey)
        Else
            DT = List.GetPollQuestions()
        End If

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxPollRepeater
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddPollButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddPollButton.Click
        Response.Redirect("~/polls.aspx?pid=pol_addpoll")
    End Sub

    Protected Sub uxPollRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxPollRepeater.ItemDataBound
        Dim HL As HyperLink = CType(e.Item.FindControl("uxEditPollHyperlink"), HyperLink)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            If IsUserAuthenticated Then
                If IsAllowedEdit(e.Item.DataItem("userid").ToString) Then
                    HL.Visible = True
                End If
            End If

        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Private Function IsAllowed() As Boolean
        Return ((IsUserAuthenticated And EnableUserPolls) OrElse IsPollModerator())
    End Function

    Private Function IsAllowedEdit(ByVal UserID As String) As Boolean
        Return (CurrentUserID = ConvertToGuid(UserID) OrElse IsPollModerator())
    End Function

    Private Function IsAllowedDelete(ByVal UserID As String) As Boolean
        Return (CurrentUserID = ConvertToGuid(UserID) OrElse IsPollModerator())
    End Function

#End Region

End Class
